/* contains shared server/client engine backbone parts */

#include <allegro.h>
#include "setup.h"
#include "main.h"
#include "resources/resources.h"
#include "util/ipc.h"
#include "util/algorithm.h"

BITMAP* buffer;
int quit;
volatile int ticks = 0; 

void flip ()
{
	stretch_blit(buffer, screen, 0, 0, buffer->w, buffer->h, 0, 0, SCREEN_W, SCREEN_H);
	#if TOWERHACK_DEBUG_DUNGEON
	clear_bitmap (buffer);
	#endif
}

void timer ()
{
	ticks++;
}

void load_allegro ()
{
	allegro_init ();
	install_keyboard ();
	install_timer ();
	install_mouse ();
	enable_hardware_cursor (); // TODO: check capabilities?
	set_color_depth (32);
	set_alpha_blender ();
	set_gfx_mode (GFX_AUTODETECT_WINDOWED, HOST_WIDTH, HOST_HEIGHT, 0, 0);
	buffer = create_bitmap (DISPLAY_WIDTH, DISPLAY_HEIGHT);
	install_int_ex (timer, BPS_TO_TIMER(FPS));
}

void load_data ()
{
	resources_load();
	set_mouse_sprite (bmp.cursor);
}

void all_init ()
{
	algorithm_init (ALGORITHM_MEMSIZE);
}

void cleanup ()
{
	algorithm_cleanup ();
	resources_unload();
	destroy_bitmap(buffer);
	allegro_exit();
	ipc_stop_server (); // does nothing if server is not child process
}
